﻿using RXY_Mall.Application.Base;
using RXY_Mall.Application.Base.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RXY_Mall.Admin.Controllers
{
    public class ToolsController : BaseController
    {
        private IBaseService _baseService;
        public ToolsController()
        {
            _baseService = new BaseService();
        }

        //[HttpPost]
        //public JsonResult SendOrderMsg()
        //{
        //    var result = _baseService.SendOrderMsg();
        //    return Json(result);
        //}

        public ActionResult GetPrivoceList( int id=0)
        {
            return Json(_baseService.GetPrivoceListByPid(id));
        }
        public ActionResult GetCategoryList(int id = 0,int tid=1)
        {
            return Json(_baseService.GetCategoryListByPid(id, tid));
        }

        public ActionResult GetFreightTemplats(int id = 0)
        {
            return Json(_baseService.GetFreightTemplate(id));
        }

        public ActionResult Developer()
        {
            ViewBag.Right = false;
            if (MemberId==1)
            {
                ViewBag.Right = true;
            }
            return View();
        }

        #region 运费模板
        public ActionResult FreightTemplate()
        {
            return View();
        }
        public JsonResult GetFreightTemplateList(string sEcho, int iDisplayStart, int iDisplayLength, string dateMin, string dateMax, string name, int type, int status)
        {
            int totalCount = 0;
            var list = _baseService.GetAllFreightTemplateByPage(iDisplayStart, iDisplayLength, dateMin, dateMax, name, type, status, out totalCount);
            return Json(new { sEcho = sEcho, iTotalRecords = totalCount, iTotalDisplayRecords = totalCount, aaData = list }, JsonRequestBehavior.AllowGet);
        }
        //禁用/启用
        [HttpPost]
        public ActionResult SetTemplateIsShow()
        {
            string id = Request.Form["id"];
            string isShow = Request.Form["isShow"];
            int idDefault = 0;
            int isShowDefault = 0;
            if (string.IsNullOrWhiteSpace(id) || !int.TryParse(id, out idDefault))
            {
                return Json(new { status = "n", info = "ID错误" });
            }
            if (string.IsNullOrWhiteSpace(isShow) || !int.TryParse(isShow, out isShowDefault))
            {
                return Json(new { status = "n", info = "参数错误" });
            }

            var result = _baseService.SetTemplateIsShow(MemberId, idDefault, isShowDefault);
            if (result)
            {
                return Json(new { status = "y", info = "操作成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "操作失败。" });
            }
        }
        public ActionResult AddFreightTemplate()
        {
            return View();
        }

        public ActionResult EditFreightTemplate(int id = 0)
        {
            ViewBag.id = id;
            return View();
        }
        [HttpGet]
        public JsonResult GetFreightTemplate(int id = 0)
        {
            if (id > 0)
            {
                var model = _baseService.GetFreightTemplateById(id);
                if (model!=null)
                {
                    return Json(model, JsonRequestBehavior.AllowGet);
                }
            }
            return Json("", JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public async Task<JsonResult> AddFreightTemplate(FreightTemplateInput input)
        {
            var result = await _baseService.AddFreightTemplate(MemberId, input);
            if (result.code == 1)
            {
                return Json(new { status = "y", info = "保存成功。" });
            }
            else
            {
                return Json(new { status = "n", info = result.msg });
            }
        } 
        #endregion
    }
}